<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="加盟公司" prop="cooperateCompany">
          <el-input
            v-model="queryParams.cooperateCompany"
            placeholder="请输入加盟公司名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="加盟方" prop="cooperateName">
          <el-input
            v-model="queryParams.cooperateName"
            placeholder="请输入加盟方姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="合作模式" prop="cooperateMode">
          <el-select v-model="queryParams.cooperateMode" placeholder="请选择合作模式" clearable>
            <el-option
              v-for="dict in dict.type.fms_cooperate_mode"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="对接财务" prop="serviceFinance">
          <el-select v-model="queryParams.serviceFinance" placeholder="请选择对接财务" clearable>
            <el-option
              v-for="item in staffFinance"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="对接销售" prop="serviceSalesman">
          <el-select v-model="queryParams.serviceSalesman" placeholder="请选择对接销售" clearable>
            <el-option
              v-for="item in staffSalesman"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="对接组长" prop="serviceLeader">
          <el-select v-model="queryParams.serviceLeader" placeholder="请选择对接组长" clearable>
            <el-option
              v-for="item in staffLeader"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['finance:cooperate:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['finance:cooperate:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="cooperateList" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="50" align="center" fixed />
        <el-table-column label="序号" align="center" prop="cooperateId" width="50" fixed />
        <el-table-column label="加盟公司名称" align="center" prop="cooperateCompany" width="120" :show-overflow-tooltip="true" />
        <el-table-column label="加盟方姓名" align="center" prop="cooperateName" width="100" />
        <el-table-column label="加盟方电话" align="center" prop="cooperateTel" width="120" />
        <el-table-column label="加盟方电话2" align="center" prop="cooperateTel2" width="120" />
        <el-table-column label="合作模式" align="center" prop="cooperateMode" width="100" />
        <el-table-column label="对接财务" align="center" prop="serviceFinance" width="100" />
        <el-table-column label="对接销售" align="center" prop="serviceSalesman" width="100" />
        <el-table-column label="对接组长" align="center" prop="serviceLeader" width="100" />
        <el-table-column label="总合作金额" align="center" prop="totalMoney" width="100" />
        <el-table-column label="已经交付金额" align="center" prop="payMoney" width="100" />
        <el-table-column label="未交付金额" align="center" prop="notPayMoney" width="100" />
        <el-table-column label="剩余加盟费" align="center" prop="remainMoney" width="100" />
        <el-table-column label="创建者" align="center" prop="createBy" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['finance:cooperate:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['finance:cooperate:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="加盟公司名称">
                <el-input v-model="form.cooperateCompany" placeholder="请输入加盟公司名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="加盟方姓名">
                <el-input v-model="form.cooperateName" placeholder="请输入加盟方姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="加盟方电话号">
                <el-input v-model="form.cooperateTel" placeholder="请输入加盟方电话号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="加盟方电话号2">
                <el-input v-model="form.cooperateTel2" placeholder="请输入加盟方电话号2" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合作模式" prop="cooperateMode">
                <el-select v-model="form.cooperateMode" placeholder="请选择合作模式">
                  <el-option
                    v-for="dict in dict.type.fms_cooperate_mode"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="对接财务">
                <el-select v-model="form.serviceFinance" placeholder="请选择对接财务">
                  <el-option
                    v-for="item in staffFinance"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="对接销售">
                <el-select v-model="form.serviceSalesman" placeholder="请选择对接销售">
                  <el-option
                    v-for="item in staffSalesman"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="对接组长">
                <el-select v-model="form.serviceLeader" placeholder="请选择对接组长">
                  <el-option
                    v-for="item in staffLeader"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="总合作金额">
                <el-input-number v-model="form.totalMoney" placeholder="请输入"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="已交付订单金额">
                <el-input-number v-model="form.payMoney" placeholder="请输入"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="未交付订单金额">
                <el-input-number v-model="form.notPayMoney" placeholder="请输入"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="剩余加盟费">
                <el-input-number v-model="form.remainMoney" placeholder="请输入"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listCooperate, getCooperate, delCooperate, addCooperate, updateCooperate } from "@/api/finance/cooperate";
  import { listStaff } from "@/api/finance/staff";
  
  export default {
    name: "Cooperate",
    dicts: ['fms_cooperate_mode'],
    data() {
      return {
        staffFinance: [],
        staffSalesman: [], 
        staffLeader: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 公告表格数据
        cooperateList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          cooperateName: [
            { required: true, message: "加盟名称不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.loadStaffList();
    },
    methods: {
      loadStaffList() {
        listStaff({}).then(response => {
          this.staffFinance = [];
          this.staffSalesman = [];
          this.staffLeader = [];
          for(let i in response.rows){
            if(response.rows[i].staffType == '财务'){
              this.staffFinance.push({label:response.rows[i].staffName,value:response.rows[i].staffName});
            }else if(response.rows[i].staffType == '销售'){
              this.staffSalesman.push({label:response.rows[i].staffName,value:response.rows[i].staffName});
            }else if(response.rows[i].staffType == '组长'){
              this.staffLeader.push({label:response.rows[i].staffName,value:response.rows[i].staffName});
            }
          }
        });
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        listCooperate(this.queryParams).then(response => {
          this.cooperateList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          cooperateId: undefined,
          cooperateName: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.noticeId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加加盟";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const cooperateId = row.cooperateId || this.ids
        getCooperate(cooperateId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改加盟";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.cooperateId != undefined) {
              updateCooperate(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCooperate(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const cooperateIds = row.cooperateId || this.ids
        this.$modal.confirm('是否确认删除加盟编号为"' + cooperateIds + '"的数据项？').then(function() {
          return delCooperate(cooperateIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
  </script>
  